﻿using System;

namespace Medianamik.Core.CodeGeneration
{
    public class TableColumn
    {
        public string Name { get; set; }
        public SqlType SqlType { get; set; }
        public bool IsNull { get; set; }
        public string DefaultValue { get; set; }

        private string _parameterName;
        public string ParameterName
        {
            get
            {
                return _parameterName
                    ?? (_parameterName = "@" + Name.TrimStart('[').TrimEnd(']'));
            }
        }

        public TableColumn(string pName, SqlType pType, bool pIsNull)
        {
            Name = '[' + pName.TrimStart('[').TrimEnd(']') + ']';
            SqlType = pType;
            IsNull = pIsNull;
        }

        public TableColumn(IProperty pProperty)
        {
            Name = '[' + pProperty.Name + ']';
            SqlType = SqlType.FromNodeTypeProperty(pProperty);
            IsNull = true; //!pProperty.IsRequired;
        }

        public string ToCreateTableString()
        {
            return ToCreateTableString(true);
        }

        public string ToCreateTableString(bool createTable)
        {
            return createTable ? Name + " " + SqlType + " "
                   + (IsNull ? String.Empty : " NOT ")
                   + " NULL " :  Name;
        }
    }
}